/**
 * @(#) TotalIncomProxy.as
 *
 * @date		2009. 04. 23.
 * @version		1.0
 * @author		Jo,Chang Hun
 * @description	 연말정산  - Proxy
 * 				연말정산 총소득액 관리 
 */
package com.skt.common_hr.yearadjustment.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.vo.TotalIncomeVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.Hourglass;
	
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class TotalIncomeProxy extends CommonProxy
	{
		public static const NAME:String = "TotalIncomeProxy";
		public var msgStr:String;
		
		
		public var priorAreaData:ArrayCollection = new ArrayCollection();


		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function TotalIncomeProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0083");
			
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;
			param["I_PGBN"] = "1";
			loadData("Z_HR_0083_NEW", param, result, fault); 
			
		}
		
		public function save(pernr:String, year:String, priorAreaArr:Array):void
		{
			Hourglass.show(null, "데이터를 저장 중입니다.", "저장");
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_0083");
			
			/* for(var i:int=0; i < priorAreaArr.length; i++){
				trace("priorAreaArr.PCBUS : " + priorAreaArr[i].PCBUS);
			} */
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;
			param["I_PGBN"] = "2";
			var tables:Object  = new Object();
			tables["ITAB"] = priorAreaArr;
			saveData("Z_HR_0083_NEW", param, tables, saveResult, saveFault); 
			
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				trace( "suss to load totalIncom proxy");
				priorAreaData = parsePriorArea(event.result.ITAB);
				facade.sendNotification(YearAdjustmentFacade.LOAD_TOTALINCOM_AREA_COMPLETE, this);
			}
			else {
				trace( "failed to load totalIncom: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.LOAD_TOTALINCOM_AREA_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in totalIncom: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_TOTALINCOM_AREA_FAILED, this);
		}
		
		public function saveResult(event:ResultEvent):void
		{
			Hourglass.remove();
			if(event.result["RETCODE"]==0) {  
				trace( "suss to load totalIncom proxy");
				facade.sendNotification(YearAdjustmentFacade.SAVE_TOTALINCOM_AREA_COMPLETE, this);
				CursorManager.removeBusyCursor();
			}
			else {
				trace( "failed to load totalIncom: (" +event.result["RETCODE"] + ") "+event.result["RETTEXT"]);
				msgStr = event.result["RETTEXT"];
				facade.sendNotification(YearAdjustmentFacade.SAVE_TOTALINCOM_AREA_FAILED, this);
				CursorManager.removeBusyCursor();
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function saveFault(event:FaultEvent):void
		{
			Hourglass.remove();
			trace("Fault message in totalIncom: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.SAVE_TOTALINCOM_AREA_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 연말정산  MainList Value Object
		 */
		private function parsePriorArea(arr:ArrayCollection):ArrayCollection
		{
			var returnArr:ArrayCollection = new ArrayCollection();
			for each(var reg:Object in arr){
				var vo:TotalIncomeVO = new TotalIncomeVO();
				
				vo.SEQNO	= reg["SEQNO"];
				vo.BEGDA	= reg["BEGDA"];
				vo.ENDDA	= reg["ENDDA"];
				vo.PCCOM	= reg["PCCOM"];
				vo.PCBUS	= reg["PCBUS"];
				vo.PCSAL	= Number((reg["PCSAL"] * 100).toFixed(2));
				vo.PCBON	= Number((reg["PCBON"] * 100).toFixed(2));
				vo.PCOUT	= Number((reg["PCOUT"] * 100).toFixed(2));
				vo.PCOVR	= Number((reg["PCOVR"] * 100).toFixed(2));
				vo.PCOTH	= Number((reg["PCOTH"] * 100).toFixed(2));
				vo.PCNPM	= Number((reg["PCNPM"] * 100).toFixed(2));
				vo.PCMED	= Number((reg["PCMED"] * 100).toFixed(2));
				vo.PCEIM	= Number((reg["PCEIM"] * 100).toFixed(2));
				vo.PCINT	= Number((reg["PCINT"] * 100).toFixed(2));
				vo.PCRET	= Number((reg["PCRET"] * 100).toFixed(2));
				vo.PCFOR	= Number((reg["PCFOR"] * 100).toFixed(2));
				vo.PCSPT	= Number((reg["PCSPT"] * 100).toFixed(2));
				
				returnArr.addItem(vo);
			}
			return returnArr;
		}
		
	}
}